package com.tongan.entity.config;


import com.alibaba.fastjson.annotation.JSONField;
import com.tongan.database.JeecgReadTable;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.jeecgframework.web.system.pojo.base.TSTypegroup;
import org.tinygroup.commons.tools.StringUtil;

import javax.persistence.*;

/**   
 * @Title: Entity
 * @Description: 自动生成表的列属性
 * @author jueyue
 * @date 2013-06-30 11:37:32
 * @version V1.0   
 *
 */
@Entity
@Table(name = "cgform_field", schema = "")
@DynamicUpdate(true)
@DynamicInsert(true)
@JsonAutoDetect
@Data
@SuppressWarnings("serial")
public class CgFormFieldEntity implements java.io.Serializable {
	/**id*/
	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	@Column(name ="id",nullable=false,length=32)
	private String id;
	/**字段名称*/
	@Column(name ="field_name",nullable=false,length=32)
	private String fieldName;

	/**
	 * 获取实体类名
	 * @return
	 */
	public String getEntityName(){
		return StringUtils.capitalize(fieldName);
	}

	/**
	 * 获取变量形式的名
	 * @return
	 */
	public String getVariableName(){
		return StringUtils.uncapitalize(getEntityName());
	}

	/**关联的表*/

	@ManyToOne
	@JoinColumn(name ="table_id",nullable=false,referencedColumnName="id")
	@JSONField(serialize = false)
	@ForeignKey(name="null")
	private CgFormHeadEntity table;
	/**字段长度*/
	@Column(name ="length",nullable=false,precision=10,scale=0)
	private Integer length;
	/**小数点长度*/
	@Column(name ="point_length",nullable=true,precision=10,scale=0)
	private Integer pointLength;
	/**字段类型*/
	@Column(name ="type",nullable=false,length=32)
	private String type;
	/**是否允许空值*/
	@Column(name ="is_null",nullable=true,length=5)
	private String isNull;
	/**在表中的顺序*/
	@Column(name ="order_num",nullable=true,length=4)
	private Integer orderNum;
	/**是否主键*/
	@Column(name ="is_key",nullable=true,length=2)
	private String isKey;
	/**是否显示*/
	@Column(name ="is_show",nullable=true,length=5)
	private String isShow;
	/**是否显示*/

	/**
	 * 其他表需要引用的时候 显示出来的字段
	 */
	@Setter
	@Column(name = "is_main_show")
	private String isMainShow;

	/**是否在列表上显示*/
	@Column(name ="is_show_list",nullable=true,length=5)
	private String isShowList;
	/**显示类型*/
	@Column(name ="show_type",nullable=true,length=10)
	private String showType;
	/**是否生产查询字段*/
	@Column(name ="is_query",nullable=true,length=5)
	private String isQuery;
	/**控件长度*/
	@Column(name ="field_length",nullable=true,length=10)
	private Integer fieldLength;
	/**字段Href*/
	@Column(name ="field_href",nullable=true,length=200)
	private String fieldHref;
	/**控件校验*/
	@Column(name ="field_valid_type",nullable=true,length=300)
	private String fieldValidType;
	/**
	 * 字段校验是否必填
	 */
	@Column(name="field_must_input")
	private String fieldMustInput;

	/**查询类型single(默认：单字段查询),group(范围查询)*/
	@Column(name ="query_mode",nullable=true,length=10)
	private String queryMode;
	/**功能注释*/
	@Column(name ="content",nullable=false,length=200)
	private String content;
	/**创建时间*/
	@Column(name ="create_date",nullable=true)
	private java.util.Date createDate;
	/**创建人ID*/
	@Column(name ="create_by",nullable=true)
	private String createBy;
	/**创建人名称*/
	@Column(name ="create_name",nullable=true,length=32)
	private String createName;
	/**修改时间*/
	@Column(name ="update_date",nullable=true)
	private java.util.Date updateDate;
	/**修改人ID*/
	@Column(name ="update_by",nullable=true,length=32)
	private String updateBy;
	/**修改人名称*/
	@Column(name ="update_name",nullable=true,length=32)
	private String updateName;


	/**旧的字段名称*/
	@Column(name ="old_field_name",nullable=true,length=32)
	private String oldFieldName;
	/**字段默认值*/
	@Column(name ="field_default",nullable=true,length=20)
	private String fieldDefault;
	/**扩展参数**/
	@Column(name ="extend_json",nullable=true,length=500)
	private String extendJson;

	//==================字典组
	/**字典Code*/
	@Column(name ="dict_field",nullable=true,length=100)
	private String dictField;
	/**字典Table*/
	@Column(name ="dict_table",nullable=true,length=100)
	private String dictTable;
	/**字典Text*/
	@Column(name ="dict_text",nullable=true,length=100)
	private String dictText;

	@JsonIgnore
	@Transient
	private TSTypegroup dictGroup;

	//==================

	//==================关联
//	/**主表名*/
//	@Column(name ="main_table",nullable=true,length=100)
//	private String mainTable;
//
//	/**主表字段*/
//	@Column(name ="main_field",nullable=true,length=100)
//	private String mainField;


	@Transient
	private String lower_ForeignEntityName;

	@Transient
	private String lower_ForeignFieldName;

	/**
	 * 外键关联表ID
	 */
	String foreignTableId;

	/**
	 * 外键关联字段ID
	 */
	String foreignFieldId;

	/**
	 * [模版使用]关联外键表实体
	 */
	@Transient
	private CgFormHeadEntity foreignTableEntity;

	/**
	 * [模版使用]关联外键字段实体
	 */
	@Transient
	private CgFormFieldEntity foreignFieldEntity;

	/**
	 * 对应的外键表的显示字段名
	 */
	@Transient
	private String foreignShowFieldName;

	/**
	 * [模版引用]多对多时相对的另一个字段名
	 * 使用场景:
	 *   根据ID获取对应的实体列表
	 */
	@JSONField(serialize = false)
	@Transient
	private CgFormFieldEntity oppositeField;

	//==================关联




	public String getForeignEntityName() {
		if (foreignTableEntity!=null){
			String xx =foreignTableEntity.getEntityName();
			return xx;
		}
		return null;
	}

	public String getLower_ForeignFieldName() {
		if (foreignTableEntity!=null){
			String xx =foreignTableEntity.getVariableName();
			return xx;
		}
		return null;
	}

	public String getForeignFieldName() {
		if (foreignFieldEntity!=null){

			return foreignFieldEntity.getEntityName();
		}
		return null;
	}

	public String getLower_ForeignEntityName() {
		String xx= StringUtil.toCamelCase(getForeignEntityName());
		return xx;
	}

	public String getFieldDefault() {
		if (fieldDefault != null) {
			fieldDefault = fieldDefault.trim();
		}
		return fieldDefault;
	}
}
